<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ebook_child extends Controller{
	var $caption;
	var $title;
	var $iframe;
	var $view='ebook_child';
	var $table='ebook';
	var $table_type='ebook_type';
	var $table_child='ebook_child';
	
	var $rule_post=array(
		array(
			'field'=>'title',
			'label'=>'产品编号',
			'rules'=>'required|max_lenx[255]'),
		array(
			'field'=>'title_num',
			'label'=>'产品名称',
			'rules'=>'required|max_lenx[255]'),
		array(
			'field'=>'type1',
			'label'=>'产品分类',
			'rules'=>'intval'),
		array(
			'field'=>'show',
			'rules'=>'intval'),
		array(
			'field'=>'recommend',
			'rules'=>'intval'),
		array(
			'field'=>'content',
			'label'=>'产品说明',
			'rules'=>'trim'),
		array(
			'field'=>'timeline',
			'rules'=>'trim'),
		);
	var $search_post=array(
		array(
			'field'=>'title',
			'label'=>'标题',
			'rules'=>'trim'),
		array(
			'field'=>'timeline',
			'rules'=>'trim'),
	);	
	
	function ebook_child(){
		parent::Controller();
		list($this->caption,$nav,$sub)=$this->backview->menu_caption_index();
		$this->load->vars(array(
			'title'=>$this->title=strtolower(get_class()),
			'caption'=>$this->caption,
			'nav_index'=>$nav,
			'sub_index'=>$sub));
		
		$this->load->model('dataset_atom','my_model');
		$this->load->model('dataset_flow_child','my_flow');
		$this->my_model->table_name($this->table);
		$this->my_flow->set_atom($this->my_model);
		$this->my_flow->set_ctrl($this);
	}

	
	function index(){
		$type_id=$this->uri->segment(3,-1);
		$p_id=$this->uri->segment(4,-1);
		if(!($parent_rs=$this->my_model->get($p_id))){
			return $this->backview->failure($this->lang->line('item_not_found'));
		}
		$this->load->vars(array(
			'parent_rs'=>$parent_rs,
			));
		$this->my_model->table_name($this->table_child);
		$this->load->view("{$this->view}",$this->my_flow->pager(array(
			'where'=>array('type_id'=>$p_id),
			'link_method'=>'child',
			'search_field'=>'title',
		)));
	}
	
	function child_search(){
		list($keyword,$cond)=$this->my_flow->search();
		$type_id=$this->uri->segment(3,-1);
		$p_id=$this->uri->segment(4,-1);
		redirect("{$this->title}/child/$type_id/1/null/10/$cond");
	}
	
	function child_create(){
		$type_id=$this->uri->segment(3,-1);
		$p_id=$this->uri->segment(4,-1);
		$this->backview->view("ajax/{$this->view}_create",get_defined_vars());
	}
	
	function child_post(){
		$type_id=$this->uri->segment(3,-1);
		$p_id=$this->uri->segment(4,-1);
		$form=array(
			'type_id'=>$p_id,
			'show'=>1,
			'timeline'=>time(),
			);
		$this->my_model->table_name($this->table_child);
		$b=$this->input->post('upload_clear');
		if(is_array($b)){
			$this->load->helper('upload_clear');
			upload_clear($b);
		}
		$a=$this->input->post('userfile_list');
		if(is_array($a)&&count($a)>0){
			$photo=$thumb=array();
			foreach($a as $v){
				$k=$this->_url_to_path($v);
				if(empty($k)||!is_file($k)){
					continue;
				}
				if(strpos($v,'_thumb')){
					$thumb[str_replace('_thumb','',$v)]=$v;
				}else{
					$photo[$v]=1;
				}
			}
			foreach($photo as $pic=>$k){
				$tmb=(isset($thumb[$pic])?$thumb[$pic]:$pic);
				$form['photo']=$pic;
				$form['thumb']=$tmb;
				$this->my_model->post($form);
			}
		}
		
		$this->backview->success($this->lang->line('item_post_success')
			,array('继续添加'=>"ajax:{$this->title}/child_create/$type_id/$p_id"
				,'完成'=>"{$this->title}/index/$type_id/$p_id"));
	}
	
	function _url_to_path($url){
		$upurl=rtrim(config_item('upload.url'),'/\\').'/';
		$updir=rtrim(config_item('upload.dir'),'/\\').'/';
		if(empty($url)||is_int($url)){
			return '';
		}
		if(!strncasecmp($url,$upurl,strlen($upurl))){
			$url=$updir.substr($url,strlen($upurl));
		}
		return $url;
	}
	
	function child_edit(){
		$this->my_model->table_name($this->table_child);
		if($vars=$this->my_flow->edit()){
			$this->backview->view("ajax/{$this->view}_edit",$vars);
		}
	}
	
	function child_jsChangeIndex(){
		$this->my_model->table_name($this->table_child);
		$id=$this->uri->segment(4,0);
		$index=$this->uri->segment(5,0);
		
		$this->load->helper('js');
		if(!$this->my_model->put($id,array('sort_id'=> intval($index)))){
			return print js_encode(array('result'=>1,'id'=>$id));
		} else {		
			return print js_encode(array('result'=>0,'id'=>$id));
		}
	}
	
	
	function child_put(){
		$type_id=$this->uri->segment(3,-1);
		$p_id=$this->uri->segment(4,-1);

		$this->my_model->table_name($this->table_child);
	
		if(!$this->my_flow->put(array(
												
			'rule'=>$this->search_post,
			'form'=>array(
				'type_id'=>$p_id,
				'timeline'=>time(),
				),
			))){
				
			return $this->child_edit();
		}
		
		$this->backview->success($this->lang->line('item_put_success')
			,array('完成'=>"javascript:location.reload();"));
	}
	
	function child_delete(){
		$this->my_model->table_name($this->table_child);
		if(!$this->my_flow->delete($this->uri->segment(4,-1))){
			return print '{"result":1}';
		}
		echo '{"result":0}';
	}
	
	function child_toggle(){
		$this->load->helper('js');
		$this->my_model->table_name($this->table_child);
		print js_encode($this->my_flow->toggle(array(
			'id_segment'=>4,
			'column_segment'=>5,
			)));
	}
	
	function child_moveup(){
		$this->my_model->table_name($this->table_child);
		$result=$this->my_flow->moveup(
			array('where'=>array("type_id"=>$this->uri->segment(3,-1))));
		switch($result){
			case 1:
				return $this->backview->failure($this->lang->line('item_not_found'));
			case 2:
				return $this->backview->failure('已经移动到最上面了');
			case 3:
				return $this->backview->failure('移动失败，请重试');
			default:if(!is_array($result))
				return $this->backview->failure('未定义的错误');
		}
		extract($result,EXTR_OVERWRITE);
		redirect("{$this->title}/child/$type_id/$page/$order/$each/$cond");
	}
	
	function child_movedown(){
		$this->my_model->table_name($this->table_child);
		$result=$this->my_flow->movedown(
			array('where'=>array("type_id"=>$this->uri->segment(3,-1))));
		switch($result){
			case 1:
				return $this->backview->failure($this->lang->line('item_not_found'));
			case 2:
				return $this->backview->failure('已经移动到最下面了');
			case 3:
				return $this->backview->failure('移动失败，请重试');
			default:if(!is_array($result))
				return $this->backview->failure('未定义的错误');
		}
		extract($result,EXTR_OVERWRITE);
		redirect("{$this->title}/child/$type_id/$page/$order/$each/$cond");
	}
	
	function child_access(){
		if(isset($_POST['access'])){
			$_POST['access']='child_'.$_POST['access'];
		}
		$this->my_model->table_name($this->table_child);
		$ret=$this->my_flow->access(array(
			'method'=>'child',
			));
		switch($ret['result']){
			case 1:
				return $this->backview->failure('未指定的操作，可能您点击了错误的链接',$ret['goto']);
			case 2:
				return $this->backview->failure('请至少选择一个项目',$ret['goto']);
		}
		$this->{'_access_'.$_POST['access']}($_POST['checked'],$ret['goto']);
	}
	
	function _access_child_delete($ids,$goto=''){
		$this->my_model->table_name($this->table_child);
		if(!$this->my_flow->access_delete($ids)){
			return $this->backview->failure($this->lang->line('item_delete_failure'));
		}
		return $this->backview->success($this->lang->line('access_delete_success')
			,array('转到列表'=>$goto));
	}
}